﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace TBM.Tools
{
    /// <summary>
    /// 分页导航类 基类
    /// </summary>
    public class PageNavigation
    {
        private string _paramName;
        /// <summary>
        /// 受参名
        /// </summary>
        public string ParamName { get { return _paramName; } }
        /// <summary>
        /// 查询字符串
        /// </summary>
        public string QueryString { get; set; }
        /// <summary>
        /// 文本位置，导航位置与文本位置相对
        /// </summary>
        public NavAlign TextAlign { get; set; }
        /// <summary>
        /// 是否显示文本
        /// </summary>
        public bool TextDisplay { get; set; }
        /// <summary>
        /// 文本模板
        /// {count} 记录条数，{currpage} 当前页码, {pagesize} 每页记录数
        /// </summary>
        public string TextTemplate { get; set; }
        private int _pageSize;
        /// <summary>
        /// 分页大小
        /// </summary>
        public int PageSize { get { return _pageSize; } }
        public int _currPage;
        private int _pageCount;
        /// <summary>
        /// 页面总数
        /// </summary>
        public int PageCount
        {
            get
            {
                return _pageCount;
            }
        }
        /// <summary>
        /// 当前页
        /// </summary>
        public object CurrPage
        {
            get
            {
                return _currPage;
            }
            set
            {
                int _value = 1;

                if (value != null)
                {
                    if (!int.TryParse(value.ToString(), out _value))
                    {
                        _value = 1;
                    }
                }

                _currPage = _value > _pageCount ? _pageCount : _value;
                _currPage = _value < 1 ? 1 : _value;
            }
        }
        private int _displayPages;
        /// <summary>
        /// 显示分页数，0表示不现实分页，仅显示上下页
        /// </summary>
        public int DisplayPages { get { return _displayPages; } }
        private int _count;
        /// <summary>
        /// 记录总数
        /// </summary>
        public int Count
        {
            get
            {
                return _count;
            }
            set
            {
                _count = value;
                _pageCount = _count / _pageSize + (_count % _pageSize == 0 ? 0 : 1);
            }
        }

        protected void Init(string paramName, int pageSize, int count, int displayPages)
        {
            TextAlign = NavAlign.Left;
            TextDisplay = true;
            TextTemplate = "共 {count} 条记录，正在浏览第 {currpage} 页（每页 {pagesize} 条记录）";
            _pageSize = pageSize;
            _displayPages = displayPages;
            _paramName = paramName;

            CurrPage = HttpContext.Current.Request[paramName];
            Count = count;
            QueryString = "";
        }
    }
}
